﻿using System.IO;
using System.ServiceModel;
using System.Xml.Serialization;
using SharedServiceLibrary;

namespace PumpStationServiceLibrary
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class PumpStationService : IPumpStationService
    {
        private readonly PumpStation _pumpStation;
        private readonly Pump[] _pumps;

        public PumpStationService()
        {
            using (var stream = new StreamReader(@"config.xml"))
            {
                var config = (Config)new XmlSerializer(typeof(Config)).Deserialize(stream);
                _pumps = config.Pumps;

                _pumpStation = new PumpStation {Pumps = config.Pumps};
                var sim = new Simulator(_pumpStation);
                sim.Start();
            }
        }

        public WaterLevel GetWaterLevel()
        {
            return _pumpStation.WaterLevel;
        }

        public void StartPump(int pumpIndex)
        {
            _pumps[pumpIndex].Running = true;
        }

        public void StopPump(int pumpIndex)
        {
            _pumps[pumpIndex].Running = false;
        }

        public Pump[] GetPumps()
        {
            return _pumps;
        }
    }
}
